import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Title: Environment.java</p>
 *
 * <p>Description: Helper class to save all the environment variables that are
 * needed to ensure proper configuration and execution of CGI scripts
 * </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author David Beaver and Aishwarya Iyer
 * @version 1.0
 */

public class Environment {
    private Socket client;
    private String HTTP_REQUEST_METHOD;
    private String HTTP_QUERY_STRING;
    private String HTTP_SERVER_NAME;
    //private static String HTTP_GATEWAY_INTERFACE;
    private String HTTP_SERVER_PROTOCOL;
    private String HTTP_SERVER_PORT;
    private String HTTP_PATH_INFO;
    private String HTTP_PATH_TRANSLATED;
    //private static String HTTP_SCRIPT_NAME;
    //private static String HTTP_REMOTE_HOST;
    private String HTTP_REMOTE_ADDR;
    private String HTTP_AUTH_TYPE;
    //private static String HTTP_REMOTE_USER;
    //private static String HTTP_REMOTE_IDENT;
    private String HTTP_CONTENT_TYPE;
    private HashMap<String,String> environment;

    /*
     * every item of inforamtion in an HTTP request header is stroed in an environment variable
	Capitalize the name in the request header field
	Convert dashes to underscores
	Add the prefix HTTP_
     *
     */
  /**
   * Constructor for Environment object. Saves the information from the server
   * configuration file as well as from the request into a data structure so if
   * the request is for a cgi script, it can be executed properly.
   *
   * @param c HttpdConf file which contains some variables which need to be
   *   saved in order for cgi scripts to execute properly.
   * @param r Request object which contains request specific variables needed
   *   to execute cgi scripts
   * @param cl Socket used by the client to connect to the server
   */
  public Environment(HttpdConf c, Request r, Socket cl) {
        this.client = cl;
        this.environment = new HashMap<String,String>();
        this.environment.put("HTTP_REQUEST_METHOD",  r.getMethod());
        this.environment.put("HTTP_PATH_INFO", r.getURI());
        this.environment.put("HTTP_PATH_TRANSLATED",c.solveAlias(c.solveAlias(r.getURI())));
        this.environment.put("HTTP_QUERY_STRING", r.getQuery());
        this.environment.put("HTTP_SERVER_PORT",  c.getPort().toString());
        this.environment.put("HTTP_CONTENT_TYPE",  r.getMediaType());
        //this.environment.put("HTTP_SERVER_NAME",c.getName());
        this.environment.put("HTTP_SERVER_PROTOCOL",  c.getProtocol());
        this.environment.put("HTTP_REMOTE_ADDR","127.0.0.1");
  }

  /**
   * Function to save static variables into your data structure which can be
   * then used during script execution. Variables that can also be accessed
   * directly from the request object or the socket (objects passed in during
   * instantiation) can be saved here as well.
   * HINT: Most request variables that need to be saved need to have an HTTP_
   * variable name so take this into consideration when planning how to design
   * and save environment variables.
   */
  
  public void setEnv() {

  }
 
  /**
   * Function to return a data structure that contains all the environment
   * variables that were saved.
   *
   * @return String[]. Chose as data type upon return due to parsing of variable
   *   content and name which both need to be stored.
   */
  
  public HashMap<String,String> getEnv() {
    return this.environment;
  }

  /**
   * Used to add another variable to the data structure. Usually used when the
   * data has not be stored yet through the setEnv function.
   *
   * @param var String that contains both the variable name and data. Used this
   *   way for parsing purposes later on.
   */
  /*
  public void addEnvVar(String var) {

  }
 */
}